
import { DefaultLayout, GraphNode } from "@/js/components/GraphNode"
import { GraphView } from "@/js/components/GraphView"
import { Input } from "@/js/components/Input"
import { InputLinkNodeAny, InputLinkNodeBoolean, InputLinkNodeFlow, InputLinkNodeString } from "@/js/components/InputLinkNode"

export class GetAttribute extends GraphNode{
    layout?: DefaultLayout
    // target?: 
    load(){
        super.load()

        const layout = new this.DefaultLayout(this, this.graphView as GraphView, false, false)
        this.layout = layout
        this.appendChild(layout.container)

        layout.main?.left.appendChild(new InputLinkNodeAny(this, this.graphView as GraphView, {
            name: "target",
            desc: '目标(默认this)',
            model: "entry",
            showInput: false
        }))
        .appendChild(new InputLinkNodeString(this, this.graphView as GraphView, {
            name: "attrName",
            desc: '属性',
            model: "entry",
            placeholder: '属性名称'
        }))
        
        layout.main?.right.appendChild(new InputLinkNodeAny(this, this.graphView as GraphView, {
            name: "print",
            desc: '引用',
            model: 'output',
            showInput: false,
            linkMaxCount: 10000
        }))
    }
}

GetAttribute.parser =  ({node,next})=>{
    const target = next(node.enters.target, "this")
    const attrName = next(node.enters.attrName) || `"${node.userData.enters.attrName}"`

    return `${target}[${attrName}]`
}